;;==========================================================================
;; Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
;;==========================================================================

#include <bsp/bspcfg.h>
#include <kcfg.h>

#define THREAD_STACK_ORDER          1
#define PAGE_SHIFT                  (12)

    AREA |.text|,ALIGN=2,CODE, READONLY

;
;   Thread *GetCurrentThread()
;
    EXPORT GetCurrentThread

GetCurrentThread
    mov     r0, sp, lsr #(PAGE_SHIFT + THREAD_STACK_ORDER)
    mov     r0, r0, lsl #(PAGE_SHIFT + THREAD_STACK_ORDER)

    mov     pc, lr


;
;   EXTERN_C void SwitchToThread0(virtaddr_t vaStack);
;
    IMPORT  KernelStartup2
    EXPORT  SwitchToThread0

SwitchToThread0
    mov     sp, r0
    mov     lr, sp
    b       KernelStartup2


;
;   void ThreadContextSwitch(Thread *pPrevThread, Thread *pNextThread)
;
    EXPORT ThreadContextSwitch

ThreadContextSwitch
    stmfd   sp!, {r4 - r11, lr}
    mrs     ip, cpsr
    str     ip, [sp, #-4]!
    str     sp, [r0, #152]      ; offsetof(Thread, m_vaKernelStackPtr) = 152
    ldr     sp, [r1, #152]
    ldr     ip, [sp], #4
    msr     spsr, ip
    ldmfd   sp!, {r4 - r11, pc}^


;
;   void ThreadContextSwitchNoReturn(Thread *pPrevThread, Thread *pNextThread)
;
    EXPORT ThreadContextSwitchNoReturn

ThreadContextSwitchNoReturn
    ldr     sp, [r0, #152]      ; offsetof(Thread, m_vaKernelStackPtr) = 152
    ldr     ip, [sp], #4
    msr     spsr, ip
    ldmfd   sp!, {r4 - r11, pc}^


    END
